/**
 * Filename: AlbumStatusGridLayoutAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/10 14:00
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNImageHW;
import com.github.postsanf.yinian.constants.YNHWSingle;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.randomlayout.StaggeredGridLayoutManager;
import com.github.postsanf.yinian.randomlayout.TwoWayView;
import com.github.postsanf.yinian.twowayview.TwoWayLayoutManager;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 相册内动态图片布局适配器
 */
public class AlbumStatusGridLayoutAdapter extends RecyclerView.Adapter<AlbumStatusGridLayoutAdapter.ViewHolder> {
    private Context mContext;
    private TwoWayView mRecyclerView;
    private List<String> mItems;
    private ImageLoader imageLoader;
    private int screenWidth;
    private int paddingWidth;

    public AlbumStatusGridLayoutAdapter(Context mContext, TwoWayView mRecyclerView, List<String> mItems) {
        this.mItems = mItems;
        this.mContext = mContext;
        this.mRecyclerView = mRecyclerView;
        imageLoader = ImageLoader.getInstance();
        screenWidth = YNSingleton.getInstance().getScreenWidth();
        paddingWidth = DisplayUtils.dp2px(mContext, 32);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final SimpleDraweeView showpic;

        public ViewHolder(View view) {
            super(view);
            showpic = (SimpleDraweeView) view.findViewById(R.id.showpic);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.yn_item_status_grid_image, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // 判断当前是否为纵向
        boolean isVertical = (mRecyclerView.getOrientation() == TwoWayLayoutManager.Orientation.VERTICAL);

        final View itemView = holder.itemView;

        final String itemUrl = mItems.get(position);
        String paths = null;
        if (mItems.size() == 1) {
            paths = YNUrlHelper.getStatusUrl(mContext, itemUrl);
            Uri uri =Uri.parse(StringUtils.repaceImgUrl(paths));
            ImageOptHelper.showFrescoStatusSingle(uri, holder.showpic, mContext);
        } else {
            paths = YNUrlHelper.getStatusUrl(mContext, itemUrl);
            Uri uri = Uri.parse(StringUtils.repaceImgUrl(paths));
            ImageOptHelper.showFrescoStatus(uri, holder.showpic, mContext);
        }

        YNImageHW imageInfo = getWHById(position);
        double rowspan = imageInfo.getImgHeight();
        int colspan = imageInfo.getImgWidth();
        int size = (int) ((rowspan * screenWidth - paddingWidth) / 72);
        final StaggeredGridLayoutManager.LayoutParams lp =
                (StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams();

        if (!isVertical) {
            lp.span = colspan;
            lp.width = size;
            itemView.setLayoutParams(lp);
        } else {
            lp.span = colspan;
            lp.height = size;
            itemView.setLayoutParams(lp);
        }
        itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    // 根据position获取图片数据
    private YNImageHW getWHById(int position) {
        // 总计取整 与 总计取余
        int tzs = getItemCount() / 8;
        int tys = getItemCount() % 8;
        // 位置取余
        int pys = position % 8;

        int mode = 0;
        int num = 0;
        if (tzs >= 1) {
            switch (tys) {
                case 0:
                    mode = 8;
                    num = pys;
                    break;
                case 1:
                    if (position < (tzs - 1) * 8) {
                        mode = 8;
                        num = pys;
                    } else if (position < (tzs - 1) * 8 + 6) {
                        mode = 6;
                        num = pys;
                    } else {
                        mode = 3;
                        num = pys >= 6 ? pys - 6 : pys + 2;
                    }
                    break;
                case 2:
                    if (position < (tzs - 1) * 8) {
                        mode = 8;
                        num = pys;
                    } else if (position < (tzs - 1) * 8 + 7) {
                        mode = 7;
                        num = pys;
                    } else {
                        mode = 3;
                        num = pys >= 7 ? pys - 7 : pys + 1;
                    }
                    break;
                case 3:
                    if (position < tzs * 8) {
                        mode = 8;
                        num = pys;
                    } else {
                        mode = 3;
                        num = pys;
                    }
                    break;
                case 4:
                    if (position < tzs * 8) {
                        mode = 8;
                        num = pys;
                    } else {
                        mode = 4;
                        num = pys;
                    }
                    break;
                case 5:
                    if (position < tzs * 8) {
                        mode = 8;
                        num = pys;
                    } else {
                        mode = 5;
                        num = pys;
                    }
                    break;
                case 6:
                    if (position < tzs * 8) {
                        mode = 8;
                        num = pys;
                    } else {
                        mode = 6;
                        num = pys;
                    }
                    break;
                case 7:
                    if (position < tzs * 8) {
                        mode = 8;
                        num = pys;
                    } else {
                        mode = 7;
                        num = pys;
                    }
                    break;
            }
        } else {
            mode = tys;
            num = pys;
        }
        return getImgWH(mode, num);
    }

    // 根据模式和序号查询图片栅栏宽高
    private YNImageHW getImgWH(int mode, int num) {
        return YNHWSingle.getInstance().getIMGHWList().get(mode).get(num);
    }
}
